package com.amit.jsf;

import java.util.Map;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;






@ManagedBean(name="ecdemoBean")
@SessionScoped
public class ECDemoBean {
	
	String str="Java Learnig Center";

	public String getStr() {
		return str;
	}

	public void setStr(String str) {
		this.str = str;
	}
	
	public ECDemoBean()
	{
		FacesContext fc=FacesContext.getCurrentInstance();
		ExternalContext ec=fc.getExternalContext();
		ServletContext sc=(ServletContext) ec.getContext();
		
		sc.setAttribute("Country1", "INDIA");
		sc.setAttribute("Country2", "INDIA");
		
		
		HttpSession session=(HttpSession) ec.getSession(true);
		session.setAttribute("STATE1", "KA");
		session.setAttribute("STATE2", "KA");
		
		HttpServletRequest request=(HttpServletRequest) ec.getRequest();
		request.setAttribute("CITY1", "bangalore");
		request.setAttribute("CITY2", "mangalore");
		
		
		System.out.println("Data is stored successfully");
			
	}
	
	@PostConstruct
public void	myinit()
	{
		System.out.println("myinit()");
	}
	
	@PreDestroy
	public void mydestroy()
	{
		System.out.println("myDestroy()");
	}
	
	public String show()
	{
		
		System.out.println("in show()");
		FacesContext fc=FacesContext.getCurrentInstance();
		ExternalContext ec=fc.getExternalContext();
		ServletContext sc=(ServletContext) ec.getContext();
		
		System.out.println(sc.getAttribute("Country1"));
		System.out.println(sc.getAttribute("Country2"));
		
		System.out.println(".................................");
		
		HttpSession session=(HttpSession) ec.getSession(true);
		System.out.println(session.getAttribute("STATE1"));
		System.out.println(session.getAttribute("STATE2"));
		
		System.out.println(".................................");
		
		HttpServletRequest req=(HttpServletRequest) ec.getRequest();
		System.out.println(req.getAttribute("CITY1"));
		System.out.println(req.getAttribute("CITY2"));
		System.out.println("Hello");
		
		System.out.println("...................");
		
		Map<String, Object> ctxMap=ec.getApplicationMap();
		ctxMap.put("SID", "9999");
		dispalyMap2(ctxMap);
		
		System.out.println(".............");
		
		Map<String, Object> sesMap=ec.getSessionMap();
		
		sesMap.put("SID", "888");
		dispalyMap2(sesMap);
		System.out.println("............");
		
	
		Map<String, Object> reqMap=ec.getRequestMap();
		reqMap.put("SID", "777");
	dispalyMap2(sesMap);		
		
		Map<String, String> ctxparam=ec.getInitParameterMap();
		displatMap1(ctxparam);
		
		System.out.println(".. initparam");
		
		Map<String, String> reqparams=ec.getRequestParameterMap();
		displatMap1(reqparams);
		System.out.println("...requestparams");
		
		Map<String, String> reqHeaders=ec.getRequestHeaderMap();
	displatMap1(reqHeaders);
		System.out.println("......reqheader");
		
		Map<String, Object> reqcookies=ec.getRequestCookieMap();
	dispalyMap2(reqcookies);
		System.out.println("...cookies");
		
		return "show";
	}

	private void displatMap1(Map<String, String> map) {
		

	for(Map.Entry<String, String> me:map.entrySet())
		{
			System.out.println(me.getKey()+" "+me.getValue());
		}
		
	}

	private void dispalyMap2(Map<String, Object> map) {
		
		for(Map.Entry<String, Object> me:map.entrySet())
		{
			System.out.println(me.getKey()+" "+me.getValue());
		}
		
	}


}
